package com.viettel.core.dao.cms;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.viettel.core.entities.cms.District;
import com.viettel.core.entities.cms.Province;
import com.viettel.core.entities.cms.Ward;
import com.viettel.core.exceptions.DataAccessException;
import com.viettel.core.repo.IRepository;

public class ProvinceDAOImpl implements ProvinceDAO{

private IRepository repo; 
	
 	public void setRepo(IRepository repo) {
		this.repo = repo;
	}
	
	@Override
	public List<Province> getAllProvinces() throws DataAccessException {
		List<Province> list = null;
		if (list == null) {
			String sql = "select p.*, case when id in (28, 51) then 1 else 0 end orderColumn"
					+ " from province p"
					+ " order by orderColumn desc, name asc ";
			list = repo.getListBySQL(Province.class, sql, null);
		}
		return list;
	}

	@Override
	public List<District> getListDistrictByProvince(Integer provinceId)
			throws DataAccessException {
		List<District> list = null;
		if (list == null) {
			StringBuilder sql = new StringBuilder(
					" select d.*, case when name like 'Quận %' and length(name) = 6 then 3");
			sql.append(" when name like 'Quận %' then 2 ");
			sql.append(" when name like 'Thành Phố %' then 1 ");
			sql.append(" else 0 end orderColumn ");
			sql.append(" from district d where province_id = ?");
			sql.append(" order by orderColumn desc, name asc");

			List<Object> params = new ArrayList<Object>();
			params.add(provinceId);
			list = repo.getListBySQL(District.class, sql.toString(), params);
		}
		return list;
	}
	
	@Override
	public List<Ward> getListWardByDistrict(Integer districtId)
			throws DataAccessException {
		List<Ward> list = null;
		if(list == null){
			StringBuilder sql = new StringBuilder();
			sql.append("select w.*,case when name like 'Xã %' then 1 ");
			sql.append("when name like 'Phường %' and length(name) = 9 then 2 ");
			sql.append("when name like 'Phường %' then 3 ");
			sql.append("else 0 end orderColumn ");
			sql.append("from ward w where district_id = ? ");
			sql.append("order by orderColumn desc, name asc ");
			
			List<Object> params = new ArrayList<Object>();
			params.add(districtId);
			
			list = repo.getListBySQL(Ward.class, sql.toString(), params);
			
		}
		return list;
	}

	@Override
	public Province getProvinceById(Integer id) throws DataAccessException {
		try {
			return repo.getEntityById(Province.class, id);
		} catch (DataAccessException e) {
			throw new DataAccessException(e);
		}
	}

	@Override
	public District getDistrictById(Integer id) throws DataAccessException {
		try {
			return repo.getEntityById(District.class, id);
		} catch (DataAccessException e) {
			throw new DataAccessException(e);
		}
	}

	@Override
	public Ward getWardById(Integer id) throws DataAccessException {
		try {
			return repo.getEntityById(Ward.class, id);
		} catch (DataAccessException e) {
			throw new DataAccessException(e);
		}
	}

}
